<template>
  <div id="problemListView">
    <h2>题目列表</h2>
    <a-form :model="searchParams" layout="inline">
      <a-form-item field="id" label="题号" style="min-width: 240px">
        <a-input
          v-model="searchParams.id"
          placeholder="请输入要查询的题号"
          allow-clear
        />
      </a-form-item>
      <a-form-item field="title" label="题目" style="min-width: 240px">
        <a-input
          v-model="searchParams.title"
          placeholder="请输入要查询的题目"
          allow-clear
        />
      </a-form-item>
      <a-form-item field="tags" label="标签" style="min-width: 240px">
        <a-input-tag
          v-model="searchParams.tags"
          placeholder="请输入要查询的标签"
        />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" shape="round" @click="onSearch">
          <icon-search /> 搜索
        </a-button>
      </a-form-item>
    </a-form>
    <a-divider size="0" />
    <a-table
      :columns="columns"
      :data="dataList"
      @page-change="onPageChange"
      :pagination="{
        showTotal: true,
        pageSize: searchParams.pageSize,
        current: searchParams.current,
        total,
      }"
    >
      <template #title="{ record }">
        <a-link @click="onViewProblem(record)">{{ record.title }}</a-link>
      </template>
      <template #tags="{ record }">
        <a-space wrap>
          <a-tag
            v-for="(tag, index) of record.tags"
            :key="index"
            color="cyan"
            bordered
            >{{ tag }}
          </a-tag>
        </a-space>
      </template>
      <template #acRate="{ record }"
        >{{
          `${record.submitNum ? record.acceptedNum / record.submitNum : 0}% (${
            record.acceptedNum
          }/${record.submitNum})`
        }}
      </template>
      <template #createTime="{ record }">
        {{ moment(record.createTime).format("YYYY-MM-DD") }}
      </template>
    </a-table>
  </div>
</template>
<script setup lang="ts">
import { onMounted, ref, watchEffect } from "vue";
import {
  Problem,
  ProblemControllerService,
  ProblemQueryRequest,
} from "../../generated";
import message from "@arco-design/web-vue/es/message";
import { useRouter } from "vue-router";
import moment from "moment";
import { IconSearch } from "@arco-design/web-vue/es/icon";

const dataList = ref([]);
const total = ref(0);
const searchParams = ref<ProblemQueryRequest>({
  title: "",
  tags: [],
  pageSize: 10,
  current: 1,
});

const loadData = async () => {
  const res = await ProblemControllerService.listProblemVoByPageUsingPost(
    searchParams.value
  );
  if (res.code === 0) {
    dataList.value = res.data.records;
    total.value = res.data.total;
  } else {
    message.error("请求数据失败，" + res.message);
  }
};

/**
 * 分页响应
 * @param page
 */
const onPageChange = (page: number) => {
  searchParams.value = {
    ...searchParams.value,
    current: page,
  };
};
//配合分页，数据（searchParam）发生变化时，用新数据重新渲染表单
watchEffect(() => {
  loadData();
});

/**
 * 页面初始化时，请求数据
 */
onMounted(() => {
  loadData();
});

/**
 * 跳转到查看题目页面
 * @param problem
 */
const router = useRouter();
const onViewProblem = (problem: Problem) => {
  router.push({
    path: `/view/problem/${problem.id}`,
  });
};

/**
 * 搜索题目
 * 等前页面重置为1
 */
const onSearch = () => {
  searchParams.value = {
    ...searchParams.value,
    current: 1,
  };
};

const columns = [
  {
    title: "题号",
    dataIndex: "id",
  },
  {
    title: "题目",
    slotName: "title",
  },
  {
    title: "标签",
    slotName: "tags",
  },
  {
    title: "难度",
    dataIndex: "difficulty",
  },
  {
    title: "通过率（通过数/提交数）",
    slotName: "acRate",
  },
  {
    title: "创建时间",
    slotName: "createTime",
  },
];
</script>

<style scoped>
#problemListView {
  max-width: 1280px;
  margin: 0 auto;
}
</style>
